home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / heap20.arj / HEAP20_C.ARJ / HALLOC.C next >
Encoding:
C/C++ Source or Header  |  1993-10-05  |  1.6 KB  |  51 lines

  1. #include <dos.h>
  2. #include <stddef.h>
  3. #include "heap.h"
  4.  
  5. extern hptr  HeapPtr;
  6. extern struct  a_heap {
  7.   uint   handle;
  8.   ulong  size;
  9.   hptr   ptr;
  10. } *Heap;
  11.  
  12. uint Halloc(ulong bytes)
  13. {
  14.   uint  handle;
  15.  
  16.   if (HeapInitialized) {
  17.     if (bytes) {
  18.       if (bytes <= HeapUnused) {
  19.         for (handle=0; handle<HeapMaxHandles; ++handle)   /* Search handles */
  20.           if (Heap[handle].ptr == NULL)        /* Found an empty handle     */
  21.             break;
  22.  
  23.         if (handle < HeapMaxHandles) {         /* Found a valid handle      */
  24.           Heap[handle].handle = handle;        /* Store new handle          */
  25.           Heap[handle].size = bytes;           /* Store new size            */
  26.           Heap[handle].ptr = HeapPtr;          /* Store avail. ptr          */
  27.  
  28.           ++*(uint *)&HeapUsedHandles;         /* Update variables          */
  29.           *(ulong *)&HeapUnused -= bytes;
  30.           *(ulong *)&HeapUsed += bytes;
  31.  
  32.           HeapPtr += bytes;                    /* Calc next avail. ptr      */
  33.  
  34.           HeapError = 0;
  35.           return(handle+1);                    /* Return new handle         */
  36.          }
  37.         else                                   /* Too many handles are used */
  38.           HeapError = ERR_TOOMANYHAND;
  39.        }
  40.       else                                     /* Size too large            */
  41.         HeapError = ERR_SIZETOOLARGE;
  42.      }
  43.     else                                       /* Size too small            */
  44.       HeapError = ERR_SIZETOOSMALL;
  45.    }
  46.   else                                         /* Not initialized           */
  47.     HeapError = ERR_NOTINIT;
  48.  
  49.   return(0);
  50. }
  51.